package com.heitian.ssm.controller;

import com.google.gson.JsonObject;
import com.heitian.ssm.model.Album;
import com.heitian.ssm.model.Attach;
import com.heitian.ssm.model.PageBean;
import com.heitian.ssm.service.AlbumService;
import com.heitian.ssm.service.AttachService;
import com.heitian.ssm.utils.CommonUtils;
import com.heitian.ssm.utils.GsonUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 作者： 陈建峰
 * 创建时间： 17/10/18 下午1:13
 * 版本： [1.0, 17/10/18]
 * 描述： 相册接口
 */
@Controller
@RequestMapping("/album")
public class AlbumController {

    private Logger log = Logger.getLogger(AlbumController.class);

    @Resource
    AlbumService albumService;

    /**
     * 保存文件
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "/SaveAlbum", method = RequestMethod.POST)
    public void SaveAlbum(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        JsonObject paras = CommonUtils.getJson(request);
        if (paras == null) {
            response.getWriter().write(CommonUtils.createJson("0", "", null).toString());
            return;
        }
        Album album = new Album();
        if (paras.has("AlbumName")) {
            album.setAlbumName(paras.get("AlbumName").getAsString());
        } else {
            response.getWriter().write(CommonUtils.createJson("0", "参数不合法", null).toString());
            return;
        }
        if (paras.has("OwnerUserGuid")) {
            album.setOwnerUserGuid(paras.get("OwnerUserGuid").getAsString());
        } else {
            response.getWriter().write(CommonUtils.createJson("0", "参数不合法", null).toString());
            return;
        }
        if (paras.has("IsPublic")) {//0不公开   1公开
            album.setIsPublic(paras.get("IsPublic").getAsString());
        } else {
            response.getWriter().write(CommonUtils.createJson("0", "参数不合法", null).toString());
            return;
        }
        if (paras.has("Content")) {
            album.setContent(paras.get("Content").getAsString());
        } else {
            response.getWriter().write(CommonUtils.createJson("0", "参数不合法", null).toString());
            return;
        }
        if (paras.has("Mark")) {
            album.setMark(paras.get("Mark").getAsString());
        } else {
            response.getWriter().write(CommonUtils.createJson("0", "参数不合法", null).toString());
            return;
        }
        if (paras.has("CoverUrl")) {
            album.setCoverUrl(paras.get("CoverUrl").getAsString());
        } else {
            response.getWriter().write(CommonUtils.createJson("0", "参数不合法", null).toString());
            return;
        }
        album.setAlbumGuid(CommonUtils.GetUUID());
        album.setUpdateTime(CommonUtils.getCurrentTime());
        album.setCreateTime(CommonUtils.getCurrentTime());
        album.setSeeTimes("0");
        album.setAllCount("0");
        album.setZanNum("0");
        albumService.saveAlbum(album);
        response.getWriter().write(CommonUtils.createJson("1", "添加成功！", null).toString());
    }

    /**
     * 删除文件
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "/DeleteAlbum", method = RequestMethod.POST)
    public void DeleteAlbum(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        JsonObject paras = CommonUtils.getJson(request);
        if (paras == null) {
            response.getWriter().write(CommonUtils.createJson("0", "", null).toString());
            return;
        }
        String AlbumGuids = paras.get("AlbumGuids").getAsString();
        String[] attachGuids = AlbumGuids.split(";");
        for (int i = 0; i < attachGuids.length; i++) {
            albumService.deleteAlbum(attachGuids[i]);
        }
        response.getWriter().write(CommonUtils.createJson("1", "删除成功！", null).toString());
    }

    /**
     * 获取某人的所有相册数量
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "/getAllAlbumCountByOwnerUserGuid", method = RequestMethod.POST)
    public void getAllAlbumCountByOwnerUserGuid(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        JsonObject paras = CommonUtils.getJson(request);
        if (paras == null) {
            response.getWriter().write(CommonUtils.createJson("0", "", null).toString());
            return;
        }
        String OwnerUserGuid = paras.get("OwnerUserGuid").getAsString();
        int count = albumService.getAllAlbumCountByOwnerUserGuid(OwnerUserGuid);
        Map<String,String> map = new HashMap<String, String>();
        map.put("count",count+"");
        String UserArea = GsonUtil.GsonString(map);
        response.getWriter().write(CommonUtils.createJson("1", "", UserArea).toString());
    }



    /**
     * 分页获取相册数据
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "/getAllAlbumByOwnerUserGuid", method = RequestMethod.POST)
    public void getAllAlbumByOwnerUserGuid(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        JsonObject paras = CommonUtils.getJson(request);
        if (paras == null) {
            response.getWriter().write(CommonUtils.createJson("0", "", null).toString());
            return;
        }
        String OwnerUserGuid = paras.get("OwnerUserGuid").getAsString();
        String index = paras.get("index").getAsString();
        String currentPageCount = paras.get("currentPageCount").getAsString();
        PageBean<Album> pageBean = albumService.getAllAlbumByOwnerUserGuid(OwnerUserGuid,Integer.parseInt(index),Integer.parseInt(currentPageCount));
        String UserArea = null;
        String code = "0";
        String message = "";
        if (pageBean != null) {
            code = "1";
            UserArea = GsonUtil.GsonString(pageBean);
        } else {
            message = "用户数据获取失败！";
        }
        response.getWriter().write(CommonUtils.createJson(code, message, UserArea).toString());
    }

    /**
     * 获取相册所有数量
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "/getAllAlbumCount", method = RequestMethod.POST)
    public void getAllAttachCount(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        JsonObject paras = CommonUtils.getJson(request);
        if (paras == null) {
            response.getWriter().write(CommonUtils.createJson("0", "", null).toString());
            return;
        }
        int count = albumService.getAllAlbumCount();
        Map<String,String> map = new HashMap<String, String>();
        map.put("count",count+"");
        String UserArea = GsonUtil.GsonString(map);
        response.getWriter().write(CommonUtils.createJson("1", "", UserArea).toString());
    }

    /**
     * 获取某个相册的信息
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "/getAlbumByAlbumGuid", method = RequestMethod.POST)
    public void getAlbumByAlbumGuid(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        JsonObject paras = CommonUtils.getJson(request);
        if (paras == null) {
            response.getWriter().write(CommonUtils.createJson("0", "", null).toString());
            return;
        }
        String AlbumGuid = paras.get("AlbumGuid").getAsString();
        Album album = albumService.getAlbumByAlbumGuid(AlbumGuid);
        String UserArea = null;
        String code = "0";
        String msg = "";
        if (album != null) {
            code = "1";
            UserArea = GsonUtil.GsonString(album);
        }else{
            msg = "未找到文件";
        }
        response.getWriter().write(CommonUtils.createJson(code, msg, UserArea).toString());
    }
}
